<?php
class WebServiceClient {
	private $url;
	private $key;
	private $version;
	private $assoc;

	function __construct($url, $key, $version = '0.0.0', $assoc = false) {
		$this->url = $url;
		$this->key = $key;
		$this->version = $version;
		$this->assoc = $assoc;
	}

	function invoke($function_name, $parameters, $files=array(), $options=array()) {
		$request = new HttpRequest($this->url, HttpRequest::METH_POST);
		$parameters = json_encode($parameters);
		$signature = sha1($this->key.sizeof($files).$function_name.$parameters);

		$option1 = array(
			'function_name' => $function_name,
			'parameters' => $parameters,
			'signature' => $signature,	
			'version' => $this->version
		);
		$post_fields = array_merge($option1, $options);

		$request->addPostFields($post_fields);
		foreach ($files as $k => $v) {
			$request->addPostFile($k, $v);
		}

		$request->send();
		if ($request->getResponseCode() == 200) {
			return json_decode(trim($request->getResponseBody()), $this->assoc);
		} else {
			return -1;
		}
	}
}
?>